import Vue from 'vue'
import store from '../store/index'
// import {SocketMessage} from './SocketMessage'
export default function () {
    let userJsonStr = sessionStorage.getItem('user')
    if (!userJsonStr) {
        return
    }
    let time
    let userEntity = JSON.parse(userJsonStr)
    const ws = new WebSocket(`ws://127.0.0.1:8888/socketServer/${userEntity.id}`)
    ws.onopen = () => {
        // Web Socket 已连接上，使用 send() 方法发送数据
        console.log('数据发送中onopen...' + ws.readyState)
        if (ws.readyState === 1) {
            store.state.websocketState = true
            time = setInterval(() => ws.send(JSON.stringify({message: 'PONG', body: `id=${userEntity.id},phone=${userEntity.phone}`})), 5000)
        }
    }
    ws.onmessage = evt => {
    }
    ws.onclose = function () {
        // 关闭 websocket
        console.log('连接已关闭...')
        clearInterval(time)
        store.state.websocketState = false
    }
    Vue.prototype.ws = ws
}
